package com.monkey.tensquare.gathering.controller;

import com.monkey.tensquare.gathering.entity.Gathering;
import com.monkey.tensquare.gathering.service.GatheringService;
import entity.Result;
import entity.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/gathering")
public class GatheringController {
    @Autowired
    private GatheringService gatheringService;
    @PostMapping
    public Result add(@RequestBody Gathering gathering){
        try {
            gatheringService.add(gathering);
            return new Result(StateCode.OK,true,"添加成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"添加失败！",null);
        }
    }
    @GetMapping
    public Result findAll(){
        try {
            List<Gathering> gatheringList = gatheringService.findAll();
            return new Result(StateCode.OK,true,"查找成功！",gatheringList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查找失败！",null);
        }
    }
    @GetMapping("{gatheringId}")
    public Result findById(@PathVariable String gatheringId){
        try {
            Gathering gathering = gatheringService.findById(gatheringId);
            return new Result(StateCode.OK,true,"查找成功！",gathering);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查找失败！",null);
        }
    }
    @PutMapping("{gatheringId}")
    public Result update(@PathVariable String gatheringId,@RequestBody Gathering gathering){
        try {
            gatheringService.update(gatheringId,gathering);
            return new Result(StateCode.OK,true,"修改成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"修改失败！",null);
        }
    }
    @DeleteMapping("{gatheringId}")
    public Result delete(@PathVariable String gatheringId){
        try {
            gatheringService.delete(gatheringId);
            return new Result(StateCode.OK,true,"删除成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"删除失败！",null);
        }
    }
}
